﻿using IPC.ListConnect.Profinet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IPC.WinForm.BaseForm
{
    public partial class FrmNewTag : Form
    {
        #region 属性
        /// <summary>
        /// 名称
        /// </summary>
        public string TagName { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string TagRemark { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        public string TagAddress { get; set; }
        /// <summary>
        /// 数据类型
        /// </summary>
        public DataType TagDataType { get; set; }
        /// <summary>
        /// 客户端访问类型
        /// </summary>
        public ReadWrite ReadWrite { get; set; }
        /// <summary>
        /// 扫描速率
        /// </summary>
        public int Frequency { get; set; }
        /// <summary>
        /// 缩放
        /// </summary>
        public double Zoom { get; set; }
        /// <summary>
        /// 主键
        /// </summary>
        public string TagGuid { get; set; }

        #endregion

        public FrmNewTag()
        {
            InitializeComponent();

            foreach (string item in typeof(DataType).GetEnumNames())
            {
                this.cbDataType.Items.Add(item);
            }

            foreach (string item in typeof(ReadWrite).GetEnumNames())
            {
                this.cbReadWrite.Items.Add(item);
            }
        }

        public FrmNewTag(string name,string remark,string address, DataType dataType, ReadWrite readWrite, int frequency,double zoom,string guid)
        {
            InitializeComponent();
            TagGuid = guid;
            this.txtName.Text = name;
            //this.txtName.Enabled = false;
            foreach (string item in typeof(DataType).GetEnumNames())
            {
                this.cbDataType.Items.Add(item);
            }
            this.cbDataType.Text = dataType.ToString();

            foreach (string item in typeof(ReadWrite).GetEnumNames())
            {
                this.cbReadWrite.Items.Add(item);
            }
            this.cbReadWrite.Text = readWrite.ToString();

            this.txtRemark.Text = remark;
            this.txtAddress.Text = address;
            this.txtFrequency.Text = frequency.ToString();
            this.txtZoom.Text = zoom.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.txtName.Text))
            {
                MessageBox.Show("请填写标签名称");
                return;
            }

            if (String.IsNullOrWhiteSpace(this.txtAddress.Text))
            {
                MessageBox.Show("请填写标签地址");
                return;
            }

            if (String.IsNullOrWhiteSpace(this.cbDataType.Text))
            {
                MessageBox.Show("请选择数据类型");
                return;
            }

            if (String.IsNullOrWhiteSpace(this.cbReadWrite.Text))
            {
                MessageBox.Show("请选择客户端访问类型");
                return;
            }
            if(TagGuid == null|| TagGuid == string.Empty)
            {
                TagGuid = Guid.NewGuid().ToString("N");
            }
            TagName = this.txtName.Text;
            TagRemark = this.txtRemark.Text;
            TagAddress = this.txtAddress.Text;
            TagDataType = (DataType)Enum.Parse(typeof(DataType), this.cbDataType.Text);
            ReadWrite = (ReadWrite)Enum.Parse(typeof(ReadWrite), this.cbReadWrite.Text);
            int frequency = 1000;
            int.TryParse(this.txtFrequency.Text, out frequency);
            Frequency = frequency;
            double zoom = 1;
            double.TryParse(this.txtZoom.Text, out zoom);
            Zoom = zoom;
            this.DialogResult = DialogResult.OK;
        }
    }
}
